package VWorldGameStudio;

import FCRendering.FCRMaster;
import jdk.nashorn.internal.objects.annotations.Setter;
import lombok.Getter;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class FCCharacter extends Sprite{
	//人物ID
	private int id; 
	//人物名称
	private String name;
	//人物类型
	private String type;
	//人物行为id
	private String actionID;
	//触发事件ID
	private String eventID="null";
	//动作素材
	private String pngurl;
	//向上行走动画
	private Image[] ActionUp=new BufferedImage[4];
	//向下行走动画
	private Image[] ActionDown=new BufferedImage[4];
	//向左行走动画
	private Image[] ActionLeft=new BufferedImage[4];
	//向右行走动画
	private Image[] ActionRight=new BufferedImage[4];
	//备战素材
	private String pngurlbeizhan;
	private Image pngbeizhan;
	//徒手攻击素材
	private String pngurltushou;
	private Image pngtushou;
	//弹弓攻击素材
	private String pngurldangong;
	private Image pngdangong;
	//其他武器攻击素材
	private String pngurlqita;
	private Image pngqita;
	@Getter
	private Image pngThrowReady;
	@Getter
	private Image pngThrowEnd;
	@Getter
	private Image pngDao;
	@Getter
	private Image pngHuoPao;
	@Getter
	private Image pngbadao;

	private static String deadUrl;
	//向上行走动画
	private static Image[] deadUp=new BufferedImage[4];
	//向下行走动画
	private static Image[] deadDown=new BufferedImage[4];
	//向左行走动画
	private static Image[] deadLeft=new BufferedImage[4];
	//向右行走动画
	private static Image[] deadRight=new BufferedImage[4];
	/**
	 * 注册
	 * @param id
	 * @param name
	 * @param type
	 * @param actionID
	 * @param eventID
	 * @param pngurl
	 */
	public void registerCharater(int id,String name,String type,String actionID,String eventID,String pngurl) {
		this.actionID=actionID;
		this.eventID=eventID;
		this.id=id;
		this.name=name;
		this.pngurl=pngurl;
		this.type=type;
		String realPngName =pngurl.substring(pngurl.lastIndexOf("/")+1, pngurl.lastIndexOf("."));
		System.out.println("realPngName "+realPngName);
		pngurlbeizhan="./material/png/character/"+realPngName+"-beizhan.png";
		pngurltushou="./material/png/character/"+realPngName+"-tushou.png";
		pngurldangong="./material/png/character/"+realPngName+"-slingshot.png";
		pngurlqita="./material/png/character/"+realPngName+"-pistol.png";

		initializationAction();
		pngbeizhan=Toolkit.getDefaultToolkit().getImage(pngurlbeizhan);
		pngtushou=Toolkit.getDefaultToolkit().getImage(pngurltushou);
		pngdangong=Toolkit.getDefaultToolkit().getImage(pngurldangong);
		pngqita=Toolkit.getDefaultToolkit().getImage(pngurlqita);
		pngqita=Toolkit.getDefaultToolkit().getImage(pngurlqita);
		pngThrowReady=Toolkit.getDefaultToolkit().getImage("./material/png/character/"+realPngName+"-throwReady.png");
		pngThrowEnd=Toolkit.getDefaultToolkit().getImage("./material/png/character/"+realPngName+"-throwEnd.png");
		pngDao = Toolkit.getDefaultToolkit().getImage("./material/png/character/"+realPngName+"-dao.png");
		pngHuoPao = Toolkit.getDefaultToolkit().getImage("./material/png/character/"+realPngName+"-huoPao.png");
		pngbadao = Toolkit.getDefaultToolkit().getImage("./material/png/character/"+realPngName+"-badao.png");

	}
	
	/**
	 * 更新
	 * @param id
	 * @param name
	 * @param type
	 * @param actionID
	 * @param eventID
	 * @param pngurl
	 */
	public void updataCharacter(int id,String name,String type,String actionID,String eventID,String pngurl) {
		this.actionID=actionID;
		this.eventID=eventID;
		this.id=id;
		this.name=name;
		this.pngurl=pngurl;
		this.type=type;
	}
	
	/**
	 * 初始化四个方向行走动画 
	 */
	private void initializationAction() {
		try {
			BufferedImage oldpng=ImageIO.read(new File(pngurl));
			for(int i=0;i<4;i++) {
				for(int j=0;j<4;j++) {
					BufferedImage newpng=oldpng.getSubimage(j*32, i*32, 32, 32);
					switch(i) {
					case 0:
						ActionDown[j]=newpng;
						break;
					case 1:
						ActionLeft[j]=newpng;
						break;
					case 2:
						ActionRight[j]=newpng;
						break;
					case 3:
						ActionUp[j]=newpng;
						break;
					}
				}
			}
			if(deadUrl == null) {//公用死亡图片
				deadUrl = "./material/png/character/dead.png";
				BufferedImage dead = ImageIO.read(new File(deadUrl));
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						BufferedImage deadpng = dead.getSubimage(j * 32, i * 32, 32, 32);
						switch (i) {
							case 0:
								deadDown[j] = deadpng;
								break;
							case 1:
								deadLeft[j] = deadpng;
								break;
							case 2:
								deadRight[j] = deadpng;
								break;
							case 3:
								deadUp[j] = deadpng;
								break;
						}
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(3);
		}
	}
	
	/**
	 * 获取备战图片
	 * @return
	 */
	public Image getBeiZhan() {
		return pngbeizhan;
	}
	
	/**
	 * 获取徒手攻击图片
	 * @return
	 */
	public Image getTuShou() {
		return pngtushou;
	}
	
	/**
	 * 获取弹弓攻击图片
	 * @return
	 */
	public Image getDanGong() {
		return pngdangong;
	}
	
	/**
	 * 获取其他武器攻击图片
	 * @return
	 */
	public Image getQiTa() {
		return pngqita;
	}
	
	/**
	 * 获取行走动作图片
	 * @param direction
	 * @param num
	 * @return
	 */
	public Image getActionImage(int direction,int num, boolean isDead) {
		if(isDead){
			switch (direction) {
				case 0:
					return deadUp[num];
				case 1:
					return deadLeft[num];
				case 2:
					return deadRight[num];
				default:
					return deadUp[num];
			}
		}else {
			switch (direction) {
				case 0:
					return ActionDown[num];
				case 1:
					return ActionLeft[num];
				case 2:
					return ActionRight[num];
				default:
					return ActionUp[num];
			}
		}
	}
	
	/**
	 * 获取人物名称
	 * @return
	 */
	public String getCharacterName() {
		return name;
	}
	
	/**
	 * 获取人物类型
	 * @return
	 */
	public String getCharacterType() {
		return type;
	}
	
	/**
	 * 获取人物行为ID
	 * @return
	 */
	public String getCharacterActionID() {
		return actionID;
	}
	
	/**
	 * 获取人物触发事件ID
	 * @return
	 */
	public String getCharacterEventID() {
		if(FCRMaster.gameSave.getChangeNpcSays().containsKey(name)){
			return FCRMaster.gameSave.getChangeNpcSays().get(name);
		}
		return eventID;
	}
	
	/**
	 * 获取人物动作素材
	 * @return
	 */
	public String getCharacterPngurl() {
		return pngurl;
	}



	/**
	 * 这啥
	 */
	public void FCRun() {}
}
